$(function(){
	dictionaryStart("model","108001");
	dictionaryStart("productionfactory","108002");
	//xlStart();
	$("#linecode").bind('change',function(){
		gtStart();
	});
	$("#towercode").bind('change',function(){
		fallow();
	});
	$("#thenumber").bind('blur',function(){
		gettotalcapacity();
	});
	var requiredColumn = [
		"devicename"
		/*,"mainlinecode"
		,"localcity"
		,"operationunit"
		,"maintenanceteam"
		,"voltagelevel"
		,"commissioningdate"
		,"isgenerationdimension"
		,"isruralpowernetwork"
		,"equipmentstate"
		,"operationstate"
		,"regionalcharacteristics"
		,"importantdegree"
		,"equipmentowner"
		,"assetsnature"
		,"assetnumber"
		,"fusespecifications"
		,"ratedvoltage" 
		,"ratedcapacity"
		,"ratedshortcircuitcur"*/
	];
	addrequired(requiredColumn);
});
var modelStart=function(){
	$.ajax({
		url : contextPath+"user/dictionary/list",
		data : {
			typeId : '13001'
		},
		type : 'POST',
		success:function(list){
			if(null!=list && list.length>0){
				$("#model").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					option += '<option value="'+list[i].dictionaryValue+'">'+list[i].dictionaryValue+'</option>';
				}
				$('#model').append(option);
			}
		}		
	});
};

var gtStart=function(){
	var linecode=$("#linecode").val();
	$.ajax({
		url : contextPath+'user/zygt/dlist',
		type : 'post',
		data : {
			companyId : companyId,
			deviceid : linecode
		},
		async : false,
		success : function(data){
			if(data!=null&&data.length>0){
				$("#towercode").empty().show();
				var option="";
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#towercode").append(option);
			}
		}
	});
	/*$.ajax({
		url : contextPath+'user/zygt/load',
		type : 'post',
		data : {
			deviceId : linecode
		},
		success :function(data){
			if(data!=null){
				$("#voltagelevel").val(data.voltagelevel);
			}
		}
	});*/
}

function fallow(){
	var gtId=$('#towercode').val();
	deviceid=gtId;
	$.ajax({
		url : contextPath+'user/zygt/load',
		data : {
			deviceid : gtId
		},
		type : 'post',
		contentType: "application/x-www-form-urlencoded; charset=utf-8", 
		success : function(data){
			if(data!=null){
				$('#localcity').val(data.localcity);
				$('#operationunit').val(data.operationunit);
				$('#maintenanceteam').val(data.maintenanceteam);
			}
		}
	});
}

function add(){
	if(!validateForm()){
		return;
	}
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zyzsdlsrdq/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/z/zyzsdlsrdqlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	if(!validateForm()){
		return;
	}
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zyzsdlsrdq/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/z/zyzsdlsrdqlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}

function gettotalcapacity(){
	var singlecapacity=$('#singlecapacity').val();
	if(singlecapacity==null||singlecapacity==""){
		alert("请输入单只容量");
		return;
	}
	var thenumber=$("#thenumber").val();
	$("#groupcapacity").val(thenumber*parseFloat(singlecapacity));
	
}
function validateForm(){
	if($("#devicename").val()==""){
		alert("设备名称为空");
		return false;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return false;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return false;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return false;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return false;
	}
	if($("input[name='equipmentowner']").val()==""){
		alert("设备主人为空");
		return false;
	}
	if($("input[name='assetnumber']").val()==""){
		alert("资产编号为空");
		return false;
	}
	if(!isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压不正确，如220v或1kv");
		return false;
	}
	if(!isNum($("input[name='ratedshortcircuitcur']").val())){
		alert("额定短路开断电流只能为数字");
		return false;
	}
	if(!validateNum($("input[name='maxshortcircuitcur']").val())){
		alert("开断容量只能为数字");
		return false;
	}
	if(!validateInt($("input[name='overvoltagetimes']").val())){
		alert("过电压倍数只能为数字");
		return false;
	}*/
	return true;
}